<?php

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\GoodsBasicInfo as mGoodsBasicInfo;
use Biz\service\GoodsDetail;
use Doraemon\model\SkuAttributes as mSkuAttributes;

$goodsIds = mGoodsBasicInfo::select("goods_id", []);
if ($goodsIds) {
    $goodsList = GoodsDetail::GoodsInfoList($goodsIds);
    foreach ($goodsList as $goods) {
        $wikiId = $goods['wiki_id'];
        if ($wikiId) {
            //同步材质和尺寸
            $material = [];
            $size = [];

            if ($goods['spec_params']) {//材质
                $materialArr = [];
                foreach ($goods['spec_params'] as $row) {
                    $materialArr[] = ['key' => $row['title'], 'value' => $row['content']];
                }
                $material = [
                    "spu_id" => $wikiId,
                    "dimension" => mSkuAttributes::TYPE_MATERIAL,
                    "detail" => json_encode($materialArr, JSON_UNESCAPED_UNICODE),
                    "status" => mSkuAttributes::STATUS_NORMAL
                ];
            }
            if ($goods['extinfo'] && $goods['extinfo'][0] != '[]' && $goods['extinfo'][0] != 'null') {//尺寸
                $size = [
                    "spu_id" => $wikiId,
                    "dimension" => mSkuAttributes::TYPE_SIZE,
                    "detail" => json_encode($goods['extinfo'], JSON_UNESCAPED_UNICODE),
                    "status" => mSkuAttributes::STATUS_NORMAL
                ];
            }
            mSkuAttributes::addSkuInfos($wikiId, [array_merge($material, $size)]);
        }
    }
}